var ToKenList = {
	tpl : null,
	data : new Hash(),
	account : "",
	load : function () {
		var qs =  new QueryString();
		ToKenList.account = qs.get("account");
		this.eventRegister();
		
		waitShow("Loading....");
		this.getSettle(ToKenList.account);
		
	},
	
	
	eventRegister : function () {
		Ext.get("btnAdd").on('click',this.addSettle); 
		Ext.get("btnConfirm").on('click',this.confirm);
		Ext.get("btnCancel").on('click',this.cancel);
		Ext.get("btnClose").on('click',this.close);
		
	},
	
	close : function () {
		window.close();
	},
	
	cancel : function () {
		if (!confirm("是否取消?")) return;
		waitShow("取消中,請稍待...");
		ToKenList.getSettle(ToKenList.account);
		
	},
	
	confirm : function () {		
		
		if (ToKenList.data.getSize() == 0) {
			alert("無資料可更新!");
			return;
		}
		
		if (!confirm("確認送交資料,?")) return;
	 	
		var items =  ToKenList.data.toArray();
		 
		for (var key in items ) {
			if (key == "remove") continue;
			items[key].sum =   parseInt(items[key].count) - parseInt(ToKenList.countData.get(items[key].rowId));	
		}
		
		waitShow("送交中,請稍待...")
	 
		Ext.Ajax.request({
			url : "/ad/sys/com/control/SettleDetailService.php?action=batchUpdate",
			method :"POST",
			params : {
				datas :  toString(items),
				account : ToKenList.account
			},
			callback  : function (op, su, rsp) {
				alert("更新成功!");
                ToKenList.getSettle(ToKenList.account);
			}
		}); 				
	},
	
	getSettle : function (account) {
		Ext.Ajax.request({
			url : "/ad/sys/com/control/SettleDetailService.php?action=getSettleDetail&account=" + account,
			callback  : function (op, su, rsp) {
				waitHide();
				ToKenList.items =  rsp.responseText.toJSON();
                ToKenList.createTempView(ToKenList.items);
			}
		}); 
	},
	
	addSettle : function (){		 	 
	 	if (!confirm("確認新增?")) return;
	 	
	 	waitShow("新增中....");
	 	Ext.Ajax.request({
			url : "/ad/sys/com/control/SettleDetailService.php?action=insertByAccount&account=" + ToKenList.account,
			callback  : function (op, su, rsp) {
				var reuslt =  rsp.responseText.toJSON();
				if (reuslt.length >0) {
					ToKenList.getSettle(ToKenList.account);
				} else {
					alert("新增失敗!")
				}
			}
		}); 	
	},
 	
	chk : function (rowId) {
		var count = Ext.getDom("count"+rowId).value;
		var settle =  Ext.getDom("settle"+rowId).value
		
		if (isNaN(count)) {
			alert("[儲值] 請輸入數字!");
			Ext.getDom("count"+rowId).focus();
			Ext.getDom("chk"+rowId).checked = false;
			return;
		}
		
		if (count< 0 ) {
			alert("[儲值] 必須大於0!");
			Ext.getDom("count"+rowId).focus();
			Ext.getDom("chk"+rowId).checked = false;
			return;
		}
		
		if (isNaN(settle)) {
			alert("[應收] 請輸入數字!");
			Ext.getDom("settle"+rowId).focus();
			Ext.getDom("chk"+rowId).checked = false;
			return;
		}
		
		if (settle < 0) {
			alert("[應收]  必須大於0!");
			Ext.getDom("settle"+rowId).focus();
			Ext.getDom("chk"+rowId).checked = false;		
			return;
		}
		
		if (Ext.getDom("chk"+rowId).checked) {
			ToKenList.data.set(rowId, {
				rowId : rowId,
				id : Ext.getDom("id"+rowId).value,
				count  : count,
				settle : settle,
				memo :Ext.getDom("memo"+rowId).value,
				chkSettle :1
			});
			Ext.getDom("count"+rowId).disabled = true;
			Ext.getDom("settle"+rowId).disabled = true;
			Ext.getDom("memo"+rowId).disabled = true;
		} else {
			ToKenList.data.set(rowId, {
				rowId : rowId,
				id : Ext.getDom("id"+rowId).value,
				count  : count,
				settle : settle,
				memo :Ext.getDom("memo"+rowId).value,
				chkSettle :0
			});
			Ext.getDom("count"+rowId).disabled = false;
			Ext.getDom("settle"+rowId).disabled = false;
			Ext.getDom("memo"+rowId).disabled = false;
		}
		
	},
 	
	chkCount : function (rowId)  {
		var count = Ext.getDom("count"+rowId).value;
		var settle =  Ext.getDom("settle"+rowId).value
		var memo =  Ext.getDom("memo"+rowId).value;
		var chk = Ext.getDom("chk"+rowId).checked ? 1: 0;
		
		
		 
		
		if (isNaN(count)) {
			alert("[儲值] 請輸入數字!");
			Ext.getDom("count"+rowId).focus();			
			return;
		}
		
		if (count< 0 ) {
			alert("[儲值] 必須大於0!");
			Ext.getDom("count"+rowId).focus();
			return;
		}
		  
		ToKenList.data.set(rowId, {
			rowId : rowId,
			id : Ext.getDom("id"+rowId).value,
			count  : count,
			settle : settle,
			memo : memo,
			chkSettle :chk
		});
	},
	
	chkSettle : function (rowId)  {
		var count = Ext.getDom("count"+rowId).value;
		var settle =  Ext.getDom("settle"+rowId).value
		var memo =  Ext.getDom("memo"+rowId).value;
		var chk = Ext.getDom("chk"+rowId).checked ? 1: 0;
		
		
		if (isNaN(settle)) {
			alert("[應收] 請輸入數字!");
			Ext.getDom("settle"+rowId).focus();
			return;
		}
		
		if (settle < 0) {
			alert("[應收]  必須大於0!");
			Ext.getDom("settle"+rowId).focus();
			return;
		}
		  
		ToKenList.data.set(rowId, {
			rowId : rowId,
			id : Ext.getDom("id"+rowId).value,
			count  : count,
			settle : settle,
			memo : memo,
			chkSettle :chk
		});
	},
	
	chkMemo : function (rowId) {
		var count = Ext.getDom("count"+rowId).value;
		var settle =  Ext.getDom("settle"+rowId).value
		var memo =  Ext.getDom("memo"+rowId).value;
		var chk = Ext.getDom("chk"+rowId).checked ? 1: 0;
	 	
		ToKenList.data.set(rowId, {
			rowId : rowId,
			id : Ext.getDom("id"+rowId).value,
			count  : count,
			settle : settle,
			memo : memo,
			chkSettle :chk
		});
	},
	
	createTempView : function (items){
	    ToKenList.countData = new  Hash();
	    
	    for (var i=0; i <items.length;i++){
	    	ToKenList.countData.set(i+1,items[i].Count);
	    }
	    
	    ToKenList.data.clear();
	    
	    if (!ToKenList.tpl) {
			ToKenList.tpl = new Ext.XTemplate(
		    	'<table id="table">',
		    	 '<col width="72" span="3" /> <col width="71" span="2" />',
		    	 '<tr>',
				    '<th width="72" nowrap="nowrap">日期</th>',
				    '<th width="72" nowrap="nowrap">儲值</th>',
				    '<th width="72" nowrap="nowrap">應收</th>',
				    '<th width="71" nowrap="nowrap">備註</th>',
				    '<th width="71" nowrap="nowrap">已入帳確認</th>',
				  '</tr>',
		    	 '<tpl for=".">',
		            '<tr>',
		            	'<td>{CreateTime:this.formatDate}<input type="hidden" id="id{[xindex]}" value="{Id}"/></td>',
		            	'<td><input type="text" id="count{[xindex]}"  value="{Count}"  onblur="ToKenList.chkCount({[xindex]})" {ChkSettle:this.isDisable} /></td>',
		            	'<td><input type="text" id="settle{[xindex]}" value="{Settle}" onblur="ToKenList.chkSettle({[xindex]})" {ChkSettle:this.isDisable}/></td>',
		            	'<td><input type="text" id="memo{[xindex]}"   value="{Memo}"   onblur="ToKenList.chkMemo({[xindex]})"  style="width:300px"  {ChkSettle:this.isDisable}/></td>',
		            	'<td><input type="checkbox" id="chk{[xindex]}" onclick="ToKenList.chk({[xindex]})" {ChkSettle:this.isChecked} {ChkSettle:this.isDisable}/></td>',
		            '</tr>',
		          '</tpl>',
		    	  '</table>',
		    	  {
		    	  	isDisable : function (ChkSettle){
		    	  		return ChkSettle == 1 ?"disabled=true" : "";
		    	  	},
		    	  	formatDate : function (date) {
		    	  		return date.toDateString();
		    	  	},	
		    	  	isChecked : function (ChkSettle) {
		    	  		return ChkSettle == 1 ? "checked = true" : "";
		    	  	}
		    	  }
		     );
	    }  
	    ToKenList.tpl.compile(); 
	    ToKenList.tpl.overwrite("divTable", items); 	 	
	}
}


Ext.onReady(function (){
	ToKenList.load();
});